package com.utils;

import android.view.View;
import android.view.animation.*;

/**
 * 日期：2019年3月20日
 * 公司：北京子杰宝贝科技有限公司
 * 地址：北京市朝阳区朝阳农场
 * 开发者：达选文
 * <p>
 * 主体功能：动画底层封装类
 */

public class Animations {
    private static AnimationSet mAnimaSet;
    private static Animations mAnima;
    private long mDurating = 300;

    public static Animations getAnimationSet() {
        mAnimaSet = new AnimationSet(true);
        if (mAnima == null) {
            mAnima = new Animations();
        }
        return mAnima;
    }

    /**
     * 渐变动画
     *
     * @param startAlpha
     * @param endAlpha
     */
    public Animations setAlphaAnima(float startAlpha, float endAlpha) {
        AlphaAnimation mAlphaAnima = new AlphaAnimation(startAlpha, endAlpha);
        mAnimaSet.addAnimation(mAlphaAnima);
        return mAnima;
    }

    /**
     * 旋转动画
     *
     * @return
     */
    public Animations setRotateAnima(float startAngle, float endAngle) {
        RotateAnimation mRotateAnima = new RotateAnimation(startAngle, endAngle);
        mAnimaSet.addAnimation(mRotateAnima);
        return mAnima;
    }

    /**
     * 缩放动画
     *
     * @return
     */
    public Animations setScaleAnima(float formX, float toX, float fromY, float toY) {
        ScaleAnimation mScaleAnima = new ScaleAnimation(formX, toX, fromY, toY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mScaleAnima.setRepeatCount(0);
        mScaleAnima.setDuration(mDurating);
        mAnimaSet.addAnimation(mScaleAnima);
        return mAnima;
    }

    /**
     * 位移动画
     *
     * @param fromXDelta
     * @param toXDelta
     * @param fromYDelta
     * @param toYDelta
     * @return
     */
    public Animations setTranslateAnima(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {
        TranslateAnimation mTranslate = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
        mAnimaSet.addAnimation(mTranslate);
        return mAnima;
    }

    public Animations setDuration(long durating) {
        this.mDurating = durating;
        return mAnima;
    }

    public AnimationSet startAnimation(View view) {
        mAnimaSet.setFillAfter(true);
        view.startAnimation(mAnimaSet);
        return mAnimaSet;
    }

    public Animations setAnimationListener(android.view.animation.Animation.AnimationListener listener) {
        mAnimaSet.setAnimationListener(listener);
        return mAnima;
    }
}
